<?php
namespace Crm\Domain;

use Crm\Common\Pager as Pager;
use Crm\Common\MetaData as MetaData;

class Coupon
{
    /**
     * pdo连接。
     *
     * @var object
     * @access pdo
     */
    public $pdo;

    public $metaData;
    public $check_option;

    public function __construct()
    {
        $this->pdo = \PhalApi\DI()->pdo;
        $this->metaData = new MetaData();

        $this->check_option = \PhalApi\DI()->config->get('basecode.check_option');
        $this->province_code = \PhalApi\DI()->config->get('app.provinceCode'); //省
        $this->city_code = \PhalApi\DI()->config->get('app.cityCode'); //城市
    }

    /* 优惠券列表. */
    public function search($data){
        $page_info = "";//url参数
        $orderField = isset($data['sort']) ? $data['sort'] : 'id';//排序字段
        $orderValue = isset($data['sort_value']) ? $data['sort_value'] : 'desc';//排序类型
        $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 15;//分页，每页显示数量
        $offset = 0;
        $subPages=5;//每次显示的页数
        $currentPage = isset($data['page']) ? (int)$data['page'] : 1;
        if($currentPage>0) $offset=($currentPage-1)*$pageSize;

        $where = 'where 1=1 ';

        //是否有效
        $flag = isset($data['flag'])?$data['flag']:'1';
        if($flag!= ''){
            $where .= " and flag={$flag}";
            $page_info .="flag=".$flag."&";
            $filter['flag'] = $flag;
        }

        //商城ID
        $mall_id = isset($data['mall_id'])?$data['mall_id']:0;
        $where .= " and mall_id={$mall_id}";
        $page_info .="mall_id=".$mall_id."&";
        $filter['mall_id'] = $mall_id;

        //项目信息
        $project_id = isset($data['project_id'])?$data['project_id']:'';
        if($project_id!= '') {
            $where .= " and project_id={$project_id}";
            $page_info .= "project_id=" . $project_id . "&";
            $filter['project_id'] = $project_id;
        }

        //用户id
        $user_id = isset($data['user_id']) ? $data['user_id'] : 0;
        if($user_id!=0) {
            $where .= " and create_uid={$user_id}";
            $page_info .= "user_id=" . $user_id . "&";
            $filter['user_id'] = $user_id;
        }

        //积分消费方式1获取2兑换
        $category = isset($data['category'])?$data['category']:0;
        if($category!= 0){
            $where .= " and category={$category}";
            $page_info .="category=".$category."&";
            $filter['category'] = $category;
        }

        $select_columns = "select %s from wx_user_coupon %s %s %s";
        $order = "order by $orderField $orderValue";
        $limit = "limit $offset,$pageSize";
        $count = " count(id) as count ";
        $sql = sprintf($select_columns,'*',$where,$order,$limit);
        $sqlcount = sprintf($select_columns,$count,$where,'','');
        $res = $this->pdo->getAll($sql);
        $Count = $this->pdo->getRow($sqlcount);
        $recordCount = $Count['count'];
        if($data['request_type']=='ajax'){
            $page=new Pager($pageSize,$recordCount,$currentPage,$subPages,$page_info,7,'ScoreList');
        }else{
            $page=new Pager($pageSize,$recordCount,$currentPage,$subPages,$page_info,4);
        }
        $splitPageStr=$page->get_page_html();

        foreach ($res as $key=>$item){
            $res[$key]['create_date_val'] = date('Y-m-d',$item['create_date']);
        }

        $result['list'] = $res;
        $result['cnt'] = $recordCount;
        $result['filter'] = $filter;
        $result['page_info'] = $page_info;
        $result['pageStr'] = $splitPageStr;
        $result['next_page'] = $currentPage;
        $result['hasmore'] = (($pageSize*$currentPage)<=$recordCount)?1:0;
        return $result;
    }


}
